<?php
	$cs = Yii::app()->getClientScript();
	$jsMoment = 'moment.js';
	$jsDaterangepicker = 'daterangepicker.js';
	$charts = 'canvasjs.min.js';
	$cssFilename = 'daterangepicker-bs3.css';
	$cs->registerScriptFile(getAssetsUrl().'/'.$jsMoment, CClientScript::POS_HEAD);
	$cs->registerScriptFile(getAssetsUrl().'/'.$jsDaterangepicker, CClientScript::POS_HEAD);
	$cs->registerScriptFile(getAssetsUrl().'/'.$charts, CClientScript::POS_HEAD);
	$cs->registerCssFile(getAssetsUrl().'/'.$cssFilename, '');
			
	function getAssetsUrl()
	{
		$assetsPath = Yii::getPathOfAlias('ext.daterangepicker');
		$assetsUrl = Yii::app()->assetManager->publish($assetsPath, false, -1);
		return $assetsUrl;
	}

	function fooActive_w($active_w)
	{
		if ($active_w) {
			$res = '';
			switch ($active_w) {
				case '0':
					$res = "Выкл.";
					break;
				case '1':
					$res = "Вкл.";
					break;	
			}					
			return $res;	
		}
	}
?>

<?php $this->widget('bootstrap.widgets.TbButton', array('label'=>'Назад','size'=>'normal','url'=>array('admin'))); ?>

<div style="text-align:center;">

	<div style="float:left;text-align:left;">
		<br/>
		<form class="form-horizontal">
		 <fieldset>
			 <div class="input-prepend">
			   <span class="add-on"><i class="glyphicon glyphicon-calendar icon-calendar"></i></span>
			   <input type="text" style="width: 160px" data-rel="<?php echo $model->source; ?>" name="reservation" id="reservation" value="<?php echo $range; ?>" /> 
			 </div>
			 <?php $this->widget('bootstrap.widgets.TbButton', array('label'=>'OK','size'=>'normal','htmlOptions'=>array('id'=>'send'))); ?>
		 </fieldset>
		</form>	
		
		<?php $this->widget('zii.widgets.CDetailView', array(
			'data'=>$model,
			'attributes'=>array(
				'title',
				'company',
				'src',
				//'source',
				'target',
				'type',
				array(
					'name'=>'active_w',
					'value'=>fooActive_w($model->active_w),
				),					
				'd_start_w',
				'd_end_w',			
			)
		)); ?>
		
	</div> 	
	<div id="chartContainer" style="width: 600px; height: 400px;"></div>
</div>

<script type="text/javascript">
	$(function () {
	
		var rel = $('#reservation').attr("data-rel");
		var range = $('#reservation').val();	
		$.fn.getstat(rel, range);
		
		$('#reservation').daterangepicker({
			format: 'DD/MM/YYYY',
			locale: {
				applyLabel: 'Ok',
				fromLabel: 'From',
				toLabel: 'To',
				daysOfWeek: ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'],
				monthNames: ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
				firstDay: 1
			}					
		});
		
		$("#send").click(function() {
			rel = $('#reservation').attr("data-rel");
			range = $('#reservation').val();	
			$.fn.getstat(rel, range);
		});	
	});
	
	$.fn.getstat = function (rel, range) {
		$.ajax({        
			url: '<?php echo Yii::app()->createUrl('words/ajaxgetstat'); ?>',
			data: { rel: rel, range: range },
			type: 'POST',				
			success: function (json) {				
				var chart = new CanvasJS.Chart("chartContainer", {
					title: {text: range},
					data: [{ type: "pie", showInLegend: true,
					dataPoints: [					
						{  y: json.result.show, legendText:"Показы", indexLabel: "Показы"},
						{  y: json.result.click, legendText:"Переходы", indexLabel: "Переходы"}]
					}]
				});				
				chart.render();		
			},
			error: function(e) {
				console.log(e);
			},				
		});		
	}		
</script>